package com.lw.common.utils;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 返回消息封装
 *  by lw on 2024年8月3日 15:05:34
 */
@Data
public class ResultVo<T> {
    private Integer code;
    private String message;
    private T data;

    public  ResultVo(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResultVo<T> success(String message, T data) {
        return new BaseResponse<>(data,message,HttpStatus.OK.value()).build();
    }

    public static <T> ResultVo<T> success() {
        return new BaseResponse<T>(HttpStatus.OK.getReasonPhrase(), HttpStatus.OK.value()).build();
    }

    public static <T> ResultVo<T> success(T data) {
        return new BaseResponse<>(data, HttpStatus.OK.value()).build();
    }

    public static <T> ResultVo<T> error(Integer code, String message) {
        return new BaseResponse<T>(message, code).build();
    }

    public static <T> ResultVo<T> error() {
        return new BaseResponse<T>(HttpStatus.BAD_REQUEST.getReasonPhrase(), HttpStatus.BAD_REQUEST.value()).build();
    }

    public static <T> ResultVo<T> error(String message) {
        return new BaseResponse<T>(message, HttpStatus.BAD_REQUEST.value()).build();
    }

    @Override
    public String toString() {
        return "ResultVo{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public static class BaseResponse<T> implements Serializable {
        private T data;
        private String message;
        private Integer code;

        public BaseResponse() {
        }

        protected BaseResponse(T data, String message, Integer code) {
            this.data = data;
            this.message = message;
            this.code = code;
        }

        protected BaseResponse( T data,Integer code) {
            this.code = code;
            this.data = data;
        }

        protected BaseResponse(Integer code) {
            this.code = code;
        }

        protected BaseResponse(String message, Integer code) {
            this.message = message;
            this.code = code;
        }
        public ResultVo<T>  build() {
            return new ResultVo<T>(this.code, this.message, this.data);
        }
   }
}
